<template>

	<view class="home">
		<uni-search-bar @confirm="doSearch" v-model="key" />
		<!-- {{msg}} -->
		<swiper style="height: 240px;" indicator-color="#fff" indicator-active-color="#ff6700" :indicator-dots="true"
			:autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in bannerGoodList" :key="index">
				<view class="swiper-item"></view>
				<image :src="item.goods_big_logo" style="width: 100%;" mode=""></image>
			</swiper-item>

		</swiper>
		<view class="cateList">
			<view class="item" v-for="(item,index) in cateOneLst" :key="index">
				<view class="logo">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="name">{{item.name}}</view>
			</view>
		</view>
		<view class="title">精选商品</view>
		<view class="selectList">
			<view class="item" v-for="(item,index) in selectedLst" :key="index">
				<view class="pic">
					<image :src="item.goods_big_logo" mode=""></image>
				</view>
				<view class="name">{{item.name}}</view>
				<view class="msg">
					<view class="price">￥{{item.price}}</view>
					<view class="tag">精选</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	let bannerGoodList = ref([])
	let key = ref("")

	onLoad(() => {
		getBanner()
		getCateOneLst()
		getSelectedList()
	})

	function doSearch() {
		uni.showToast({
			title: key.value
		})
	}
	async function getBanner() {
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 1,
			"is_promote": 0,
			"page": 2,
			"number": 5
		})
		if (res.code == 200) {
			bannerGoodList.value = res.goodsList.map(item => {
				item.goods_big_logo = uni.$base_url + item.goods_big_logo
				return item
			})
		}
		// console.log(bannerGoodList, value)
	}
	const cateOneLst = ref([])
	async function getCateOneLst() {
		let res = await uni.$post("/categoryListLevel", {
			"level": 0
		})
		if (res.code == 200) {
			cateOneLst.value = res.result.map(item => {
				item.icon = uni.$base_url + item.icon
				return item
			})
		}

		console.log(cateOneLst.value)
	}
	const selectedLst = ref([])
	async function getSelectedList() {
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 1,
			"is_promote": 0,
			"page": 1,
			"number": 20
		})
		if (res.code == 200) {
			selectedLst.value = res.goodsList.map(item => {
				item.goods_big_logo = uni.$base_url + item.goods_big_logo
				return item
			})
		}
		console.log(selectedLst.value)
	}
	// getBanner()
	// async function getNews() {
	// 	let lst = await uni.$get("https://unidemo.dcloud.net.cn/api/news")
	// 	console.log(lst)
	// 	msg.value = lst
	// }
	// getNews()
	// uni.request({
	// 	url: 'https://www.baidu.com',
	// 	url:"https://unidemo.dcloud.net.cn/api/news",
	// 	success: (res) => {
	// 		console.log(res)
	// 	}
	// })
	// const request = function(url, method, data) {
	// 	return new Promise(resolve, reject) => {
	// 		uni.request({
	// 				url,
	// 				method,
	// 				data,
	// 				success: (res) => {
	// 					resolve(res)
	// 				},
	// 				fail: (err) => {
	// 					reject(err)
	// 				}
	// 			}
	// 		})
	// }
	// async function getNews() {
	// 	let lst = await request("https://unidemo.dcloud.net.cn/api/news")
	// 	console.log(lst)
	// }
	// getNews()

	// async function getNews() {
	// 	let lst = await request("https://unidemo.dcloud.net.cn/api/news")
	// 	console.log(lst)
	// }
</script>

<style>
	/* 商品类别样式 */
	.cateList {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-around;
	}

	.cateList>.item>.logo {
		width: 50rpx;
		height: 50rpx;
	}

	.cateList>.item>.logo>image {
		width: 100%;
		height: 100%;
	}

	.cateList>.item>.name {
		font-size: 24rpx;
	}

	/* 精选商品样式 */
	.title {
		color: #ff6700;
		text-align: center;
	}

	.selectList {
		display: flex;
		/* 换行 */
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.selectList>.item {
		/* 修改盒子模型计算方式 */
		box-sizing: border-box;
		width: 350rpx;
		padding: 15rpx;
		border: 1px solid #eee;
		/*盒子边框*/
		box-shadow: 3px 3px 3px #eee;
		/*盒子阴影*/
	}

	.selectList>.item>.pic {
		width: 312rpx;
		height: 320rpx;
		border: 1px solid #ccc;
	}

	.selectList>.item>.pic>image {
		width: 100%;
		height: 100%;
	}

	.selectList>.item>.name {
		/* 为view容器设置其中显示的文本超出2行就显示省略号。 */
		overflow: hidden;
		-webkit-line-clamp: 2;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		color: #666;
		font-size: 14px;
	}

	.selectList>.item>.msg {
		display: flex;
		justify-content: space-between;
	}

	.selectList>.item>.msg>.price {
		color: #ff6700;
	}

	.selectList>.item>.msg>.tag {
		background: #ff6700;
		color: #fff;
		font-size: 12px;
		padding: 2px 5px;
		border-radius: 5px;
	}
</style>